package com.zq.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.zq.comm.Const;
import com.zq.page.Page;
import com.zq.pojo.Eva;
import com.zq.service.EvaService;
@Controller("evaAction")
public class EvaAction implements ModelDriven,RequestAware {

	private Map<String,Object> requestMap;
	private int pageNo;
	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	@Override
	public void setRequest(Map<String, Object> requestMap) {
		// TODO Auto-generated method stub
		this.requestMap = requestMap;
	}

	private Eva eva = new Eva();
	@Override
	public Eva getModel() {
		
		return eva;
	}
	@Autowired
	private EvaService evaService;
	
	
	private String option;

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}
	//增
		public String save()
		{
			
			return evaService.add(eva);
		}
		//删
		public String delete()
		{
			Eva eva1 = evaService.findById(eva.getEid());
			return evaService.delete(eva1);
		} 
		//改
		public String update()
		{
			
			return evaService.update(eva);
		}
		//查询一条数据
		public String findById()
		{
			String msg = Const.ERROR;
			Eva eva1 =evaService.findById(eva.getEid());
			if(eva1 != null)
			{
				if (Const.UPDATE.equalsIgnoreCase(option)) {
					requestMap.put("eva", eva1);
					msg = Const.UPDATE;
				}else{// update -->dispatcher WEB-INF/views/emp_update.jsp
				List<Eva> evaList = new ArrayList<>();
				evaList.add(eva1);
				requestMap.put("evaList", evaList);
				msg = Const.SUCCESS;
				}
			}
			
			return msg;
		}
		public String findAll()
		{	
			
			 pageNo = 1;
			 
			String msg = Const.ERROR;
			System.out.println("findAll这个方法进来了");
				List<Eva> evaList = evaService.findAll(pageNo);
				System.out.println(evaList);
				if (evaList != null && evaList.size() > 0) {
					requestMap.put("evaList", evaList);
					msg = Const.SUCCESS;
			}
			return msg;
		}
		//分页
		
		public String page()
		{	
			
			Page<Eva> page = evaService.getPage(pageNo);
			
			if(pageNo>=page.getTotalPage())
			{
			pageNo = page.getTotalPage();
			}
			if(pageNo<= 1 )
			{
				pageNo = 1;
			}
			
			String msg = Const.ERROR;
			
			List<Eva> evas = evaService.getPage(pageNo).getList();
			if(evas!= null && evas.size() > 0)
			{
				requestMap.put("evaList", evas);
				msg = Const.SUCCESS; 
			}
			return msg;
		}
		//多表查询
		public String many()
		{
			String msg = Const.ERROR;
			List<Eva> evas = evaService.getManyThings();
			if(evas!=null&&evas.size() > 0)
			{
				requestMap.put("evas", evas);
				msg = Const.SUCCESS;
			}
			return msg;
		}
}
